package com.four.bmm.controller;

import com.four.bmm.entity.Block;
import com.four.bmm.entity.BlockItem;
import com.four.bmm.entity.News;
import com.four.bmm.entity.User;
import com.four.bmm.service.BlockService;
import com.four.bmm.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/UserAddNewsServlet")
public class UserAddNewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*String title=request.getParameter("title");
        String content=request.getParameter("content");
        int block=Integer.parseInt(request.getParameter("block"));
        HttpSession session=request.getSession();
        Object loginuser =session.getAttribute("loginuser");
        News news =new News();
        news.setTitle(title);
        news.setContent(content);
        news.setBlockItem();
        news.setUser();
        NewsService newsService =new NewsService();
        newsService.setNews();


        request.getRequestDispatcher("news.jsp").forward(request,response);
*/

        //1.取出当前登录用户信息
         HttpSession session = request.getSession();
         User user = (User) session.getAttribute("loginuser");

        //2.获取主贴表需要的参数:主题、内容、时间
        String title = request.getParameter("title");
        String content = request.getParameter("content");



       /* // 查板块信息
        BlockService blockService=new BlockService();
        List<Block> blocks= blockService.findAllBlock();
        int bCount=blockService.calBlockCount();
        List<BlockItem> blockItems=blockService.findAllBItem();
        int biCount=blockService.calBlockItemCount();
        request.setAttribute("bCount",bCount);
        session.setAttribute("biCount",biCount);
        request.setAttribute("blocks",blocks);
        session.setAttribute("blocks",blocks);
        request.setAttribute("blockItems",blockItems);
        session.setAttribute("blockItems",blockItems);

        //3.所属版块信息
        int bitemid = Integer.parseInt(request.getParameter("bitemid"));
        System.out.println(bitemid+"-------");
        BlockItem blockitem = new BlockItem();
        blockitem.setBitemid(bitemid);*/

        //5.把发帖时的参数封装到一个topic对象中
        News news = new News(title, content,null,null, user,null);
        //6.调用service层的方法
        NewsService newsService = new NewsService();
        boolean flag = newsService.setNews(news);
        if (flag) {
            request.getRequestDispatcher("news.jsp").forward(request,response);

        }




    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
